package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public Result submit(Map map) throws Exception{
        //调用数据库查看日期是否已经设置了预约
        String orderDate = (String) map.get("orderDate");
        //查询数据库
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if(orderSetting == null){
            //日期没有设置预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //调用数据库查看当天可预约的人数是否达到上限
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(reservations >= number){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //判断是否重复预约，重复预约则预约失败
        //通过手机查询是否为会员
        Member member = memberDao.findByTelephone((String) map.get("telephone"));
        if(member !=null){
            //说明是会员
            Order order = new Order();
            order.setMemberId(member.getId());//会员id
            order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约时间
            order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
            List<Order> list = orderDao.findByCondition(order);
            if(list != null && list.size() > 0){
                //判断是否重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            //查看当前是否为会员不是的话注册到会员表中
            member = new Member();
            member.setName((String) map.get("name"));
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setPhoneNumber((String) map.get("telephone"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //更新数据库的数据给预约人数进行加一，预约成功
        Order order = new Order();
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId"))); //套餐id
        order.setOrderDate(DateUtils.parseString2Date(orderDate)); //预约时间
        order.setMemberId(member.getId()); //会员id
        order.setOrderType((String) map.get("orderType")); //预约类型 电话预约/微信预约
        order.setOrderStatus(Order.ORDERSTATUS_NO); //预约状态（是否到诊）
        orderDao.add(order);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception{
        /**
         *  <p>体检人：{{orderInfo.member}}</p>
         <p>体检套餐：{{orderInfo.setmeal}}</p>
         <p>体检日期：{{orderInfo.orderDate}}</p>
         <p>预约类型：{{orderInfo.orderType}}</p>
         */
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            Date orderDate = (Date)map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
